<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class OrderGoods extends Model
{
    /**
     * 定义主键字段
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 定义表名，不含前缀
     *
     * @var string
     */
    protected $table      = 'wd_order_goods';

    /**
     * 定义拒绝保存的字段
     *
     * @var array
     */
    protected $guarded    = ['id'];

    /**
     * 开启laravel默认的时间记录
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * 根据订单号获得对应的数据
     * @param  [array] $data [description]
     * @return [type]      [description]
     */
    public static function getOrderList($data = null) 
    {

        if ($data == null || empty($data)) {
            return [];
        }
        //获得所有的订单号
        foreach ($data as $key => $value) {
            $order_sn[] = $value['order_sn'];
        }

        //获得订单信息
        $orderInfo = self::whereIn('order_sn',$order_sn)->get();

        foreach ($orderInfo as $key => $val) {
            
            $arr[$val->order_sn][] = $val;
        }

        foreach ($data as &$value) {
            $value['order_info'] = $arr[$value['order_sn']];
        }

        return $data;
    }
}
